View Javadoc

1   /*   Copyright (C) 2004 Finalist IT Group
2    *
3    *   This file is part of JAG - the Java J2EE Application Generator
4    *
5    *   JAG is free software; you can redistribute it and/or modify
6    *   it under the terms of the GNU General Public License as published by
7    *   the Free Software Foundation; either version 2 of the License, or
8    *   (at your option) any later version.
9    *   JAG is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   *   GNU General Public License for more details.
13   *   You should have received a copy of the GNU General Public License
14   *   along with JAG; if not, write to the Free Software
15   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   */
17  
18  /*
19   * OracleDTOGenerator.java
20   *
21   * Created on 16 augustus 2002, 13:19
22   */
23  
24  package com.finalist.tools.statementExecutorUtils;
25  
26  import com.finalist.tools.database.*;
27  
28  import java.sql.Connection;
29  import java.util.ArrayList;
30  import java.util.HashMap;
31  import java.io.File;
32  import java.io.FileWriter;
33  import java.io.IOException;
34  import java.sql.DriverManager;
35  
36  /***
37   * Generates Data Transfer Objects (Value Objects) of tables in the database.
38   *
39   *
40   * @author  hillebrand
41   */
42  public class OracleDTOGenerator {
43  
44     /***
45      * @param args the command line arguments
46      */
47     public static void main(String[] args) {
48        QueryHelper qh = new QueryHelper();
49        ArrayList tables;
50  
51        if (args.length == 0) {
52           qh.prepareStatement("SELECT TABLE_NAME FROM USER_TABLES");
53           tables = qh.executeQueryIntoHashMap();
54        }
55        else {
56           tables = new ArrayList();
57           for (int i = 0; i < args.length; i++) {
58              HashMap hm = new HashMap();
59              hm.put("TABLE_NAME", args[i]);
60              tables.add(hm);
61           }
62        }
63  
64        qh.prepareStatement("SELECT COLUMN_NAME as name, " +
65           "       DATA_TYPE as type, " +
66           "       DATA_PRECISION as precision, " +
67           "       DATA_SCALE as scale " +
68           "  FROM USER_TAB_COLUMNS " +
69           " WHERE TABLE_NAME = :TABLE_NAME");
70  
71        String packageName = "com.finalist.facturenSynchronisatie.value";
72        String saveLocation = "C:/facturenSynchronisatie/src/java/com/finalist/facturenSynchronisatie/value/";
73  
74        for (int i = 0; i < tables.size(); i++) {
75           String tableName = (String) ((HashMap) tables.get(i)).get("TABLE_NAME");
76           System.out.println(tableName);
77           qh.setBindVar("TABLE_NAME", tableName);
78           ArrayList columnList = qh.executeQueryIntoHashMap();
79           columnList = OracleRowMapper.convertDatatypes(columnList);
80           String bean = Helper.generateBeanFromColumnList(Helper.createAttNameInitCap(tableName),
81              packageName, columnList);
82           try {
83              File classFile = new File(saveLocation + Helper.createAttNameInitCap(tableName) + ".java");
84              FileWriter fw = new FileWriter(classFile);
85              fw.write(bean);
86              fw.close();
87           }
88           catch (IOException e) {
89              e.printStackTrace();
90           }
91        }
92        qh.close();
93     }
94  }